Skip to content
c++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N=80005;
constexpr int M=200005;

struct edge{
    int u,v,x;
    double t;
}e[M];

int head[N],to[M],nxt[M],w[M],tot;
void add(int u,int v,int x){
    nxt[++tot]=head[u];
    head[u]=tot;
    to[tot]=v;
    w[tot]=x;
}
int dfn[N],low[N],timer;
int scc[N],scc_cnt;
int stk[N],top;

int c[N];

int n,m,s;

void tarjan(int u){
    dfn[u]=low[u]=++timer;
    stk[++top]=u;

    for(int i=head[u];i;i=nxt[i]){
        int v=to[i];
        if(!dfn[v]){
            tarjan(v);
            low[u]=std::min(low[u],low[v]);
        }else if(!scc[v]){
            low[u]=std::min(low[u],dfn[v]);
        }
    }

    if(dfn[u]==low[u]){
        ++scc_cnt;
        int v;
        do{
            v=stk[top--];
            scc[v]=scc_cnt;
        }while(v!=u);
    }
}
void work(){
    for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
}


int f(int x,double t){
    int res=x;
    int k=t*10;
    while(x){
        x=x*k/10;
        res+=x;
    }
    return res;
}

int ans=0;

void dfs(int u,int k){
    k+=c[u];
    ans=std::max(ans,k);
    for(int i=head[u];i;i=nxt[i]){
        int v=to[i];
        dfs(v,k+w[i]);
    }
}
void fc() {
    std::cin>>n>>m;
    for(int i=0;i<m;i++){
        std::cin>>e[i].u>>e[i].v>>e[i].x>>e[i].t;
        add(e[i].u,e[i].v,e[i].x);
    }

    work();

    for(int i=1;i<=n;i++)head[i]=0;
    tot=0;

    for(int i=0;i<=m;i++){
        int u=scc[e[i].u],v=scc[e[i].v];
        if(u==v){
            c[u]+=f(e[i].x,e[i].t);
        }else{
            add(u,v,e[i].x);
        }
    }

    std::cin>>s;

    dfs(scc[s],0);

    std::cout<<ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //std::cin>>t;
    while (t--) fc();
    return 0;
}